const { SuccessModel, ErrorModel } = require('../model/responseModel');
const { getBookList, getBookChapter, } = require('../controllers/book');
const { getVocabulary, getChapterWordList, getVOCPhrase, newVOCPhrase } = require('../controllers/voc');
const { createUser, getUserInfo, addUserBook, addUserVOC, getUserVOC, updateUserVOC } = require('../controllers/user');
// 定义处理路由的函数
const handleRoute = (req, res) => {
    const method = req.method;
    const id = req.query.id;
    const body = req.body;
    const user_id = req.query.user_id;
    // 判断请求方法

    // 获取书籍列表
    if (method === 'GET' && req.path === '/api/book/list') {
        const type = req.query.type || '';
        const status = req.query.status || '1';
        const author = req.query.author || '';
        const keyword = req.query.keyword || '';
        const listDataPromise = getBookList(author, keyword, status, type);
        return listDataPromise.then((listData) => {
            return new SuccessModel(listData);
        });
    }

    // 获取书籍章节列表
    if (method === 'GET' && req.path === '/api/book/chapter') {
        const book_id = req.query.book_id;
        const chapterDataPromise = getBookChapter(book_id);
        return chapterDataPromise.then((chapterData) => {
            return new SuccessModel(chapterData);
        });
    }

    // 获取单词
    if (method === 'GET' && req.path === '/api/voc/detail') {
        const keyword = req.query.keyword;
        const vocabularyDataPromise = getVocabulary(id, keyword);
        return vocabularyDataPromise.then((vocabularyData) => {
            return new SuccessModel(vocabularyData);
        });
    }

    // 获取章节单词列表
    if (method === 'GET' && req.path === '/api/book/chapter/voc') {
        const chapter_id = req.query.chapter_id || '';
        const wordListDataPromise = getChapterWordList(chapter_id);
        return wordListDataPromise.then((wordListData) => {
            return new SuccessModel(wordListData);
        });
    }

    // 新建小程序用户信息
    if (method === 'POST' && req.path === '/api/user/new') {
        const code = body.code;
        return createUser(code).then((userId) => {
            return new SuccessModel(userId);
        });
    }

    // 获取用户信息
    if (method === 'GET' && req.path === '/api/user/info') {
        const userDataPromise = getUserInfo(id);
        userDataPromise.then((userData) => {
            return new SuccessModel(userData);
        });
    }

    // 获取单词例句
    if (method === 'GET' && req.path === '/api/voc/phrase') {
        const voc_id = req.query.voc_id;
        const phraseDataPromise = getVOCPhrase(voc_id);
        return phraseDataPromise.then((phraseData) => {
            return new SuccessModel(phraseData);
        });
    }

    // 添加单词例句
    if (method === 'POST' && req.path === '/api/voc/phrase/add') {
        const newPraseDataPromise = newVOCPhrase(body);
        return newPraseDataPromise.then((newPraseData) => {
            return new SuccessModel(newPraseData);
        });
    }

    // 添加用户书籍
    if (method === 'POST' && req.path === '/api/user/book/add') {
        const newBookDataPromise = addUserBook(body);
        return newBookDataPromise.then((newBookData) => {
            return new SuccessModel(newBookData);
        });
    }

    // 获取用户书籍
    if (method === 'GET' && req.path === '/api/user/book') {
        const bookDataPromise = getUserBook(user_id);
        return bookDataPromise.then((bookData) => {
            return new SuccessModel(bookData);
        });
    }

    // 添加用户单词
    if (method === 'POST' && req.path === '/api/user/voc/add') {
        const newVOCDataPromise = addUserVOC(body);
        return newVOCDataPromise.then((newVOCData) => {
            return new SuccessModel(newVOCData);
        });
    }

    // 获取用户单词
    if (method === 'GET' && req.path === '/api/user/voc') {
        const level = req.query.level || '0'; // 0: 未学习
        const VOCDataPromise = getUserVOC(user_id, level);
        return VOCDataPromise.then((VOCData) => {
            return new SuccessModel(VOCData);
        });
    }

    // 更新用户单词
    if (method === 'POST' && req.path === '/api/user/voc/update') {
        const updateVOCDataPromise = updateUserVOC(body);
        return updateVOCDataPromise.then((updateVOCData) => {
            if (updateVOCData)
                return new SuccessModel('更新成功');
            return new ErrorModel('更新失败');
        });
    }
}

module.exports = handleRoute;